package com.six.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.six.business.dao.BusinessMapper;
import com.six.business.feign.ShopFeign;
import com.six.business.feign.TbUserFeign;
import com.six.business.vo.CheckBusinessListVo;
import com.six.business.vo.CheckBusinessVo;
import com.six.core.entity.Business;
import com.six.core.entity.CheckBusiness;
import com.six.business.service.CheckBusinessService;
import com.six.business.dao.CheckBusinessMapper;
import com.six.core.entity.Shop;
import com.six.core.util.JwtUtil;
import com.six.core.util.WebUtil;
import com.six.core.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
* @author 汪鹏
* @description 针对表【acl_check_business】的数据库操作Service实现
* @createDate 2023-02-27 17:42:04
*/
@Service
public class CheckBusinessServiceImpl extends ServiceImpl<CheckBusinessMapper, CheckBusiness> implements CheckBusinessService{

    @Autowired
    private CheckBusinessMapper checkBusinessDao;
    @Autowired
    private BusinessMapper businessDao;
    @Autowired
    private TbUserFeign userFeign;
    @Autowired
    private ShopFeign shopFeign;

    private Integer getUserid(){
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        if (token!=null){
            Map<String, Object> map = JwtUtil.decodeJWT(token);
            return (Integer) map.get("tbUserid");
        }
        return null;
    }
    @Override
    public Result getCheckBusinessList(int currentPage, int pageSize, CheckBusinessVo checkBusinessVo) {
        //设置分页的页码以及分页显示的条数---必须在查询前设置
        PageHelper.startPage(currentPage,pageSize);
        List<CheckBusinessListVo> checkBusinessList = checkBusinessDao.findAll(checkBusinessVo);
        PageInfo<CheckBusinessListVo> pageInfo = new PageInfo<CheckBusinessListVo>(checkBusinessList);
        return new Result(200,"查询成功",pageInfo);
    }

    /** 入驻审核 */
    @Override
    public Result setBusinessState(CheckBusiness checkBusiness) {
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer empId = (Integer) map.get("empId");
        checkBusiness.setEmpid(empId);

        UpdateWrapper<CheckBusiness> wrapper = new UpdateWrapper<>();
        wrapper.set("userid",checkBusiness.getUserid());
        wrapper.set("state", checkBusiness.getState());
        wrapper.set("content", checkBusiness.getContent());
        wrapper.set("empid", checkBusiness.getEmpid());
        wrapper.set("gmt_modified", LocalDateTime.now());
        wrapper.eq("userid", checkBusiness.getUserid());
        int i = checkBusinessDao.update(new CheckBusiness(), wrapper);

        Integer state = checkBusiness.getState();
        if (state == 1){
            UpdateWrapper<Business> wrapper1 = new UpdateWrapper<>();
            wrapper1.set("state",0);
            wrapper1.eq("userid",checkBusiness.getUserid());
            businessDao.update(new Business(),wrapper1);
            userFeign.setUserIsType(checkBusiness.getUserid());
        }
        if (state == 2){
            UpdateWrapper<Business> wrapper1 = new UpdateWrapper<>();
            wrapper1.set("state",1);
            wrapper1.eq("userid",checkBusiness.getUserid());
            businessDao.update(new Business(),wrapper1);
        }
        return new Result(200,"审核成功");
    }

    /** 前台获取商家入驻审核状态接口 */
    @Override
    public Result getCheckBusinessState(Integer userid) {
        QueryWrapper<CheckBusiness> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userid);
        CheckBusiness checkBusiness = checkBusinessDao.selectOne(queryWrapper);
        return new Result(200,"查询成功",checkBusiness);
    }
}




